package com.seezoon.application.tcp.executor;

import com.seezoon.domain.valueobj.PingVO;
import com.seezoon.infrastructure.tcp.codec.JT808;
import com.seezoon.infrastructure.tcp.common.RpcContext;
import com.seezoon.infrastructure.tcp.handler.MessageHandler;
import com.seezoon.infrastructure.tcp.jt808.JT0002;
import com.seezoon.infrastructure.tcp.jt808.JT8001;
import com.seezoon.infrastructure.tcp.jt808.JT8001.ResultCode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;

/**
 * @see JT808#终端心跳
 */
@Slf4j
@RequiredArgsConstructor
@Component
public class JT002Exe implements MessageHandler<JT0002, JT8001> {

    private final ApplicationEventPublisher publisher;

    @Override
    public int requestType() {
        return JT808.终端心跳;
    }

    @Override
    public int responseType() {
        return JT808.平台通用应答;
    }

    @Override
    public JT8001 execute(JT0002 request, RpcContext rpcContext) {
        JT8001 response = new JT8001();
        response.setResponseMsgSerialNo(rpcContext.getMsgSerialNo());
        response.setMsgId(rpcContext.getMsgId());
        response.setResultCode(ResultCode.SUCCESS);
        publisher.publishEvent(new PingVO(rpcContext.getClientId()));
        return response;
    }
}
